package cn.e4j.modular.common.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.entity.SysOrgT;
import cn.e4j.modular.common.mapper.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ganjia
 * @since 2019-09-25
 */
@Service
public class QryServiceImpl extends ServiceImpl<QryMapper, SysOrgT> {
    @Autowired
    private YFQueryMapper yfQueryMapper;
    @Autowired
    public E10QueryMapper e10QueryMapper;
    @Autowired
    public GPQueryMapper gpQueryMapper;
    @Autowired
    public T100QueryMapper t100QueryMapper;

    @Transactional(readOnly = true)
    public void createTable(String tableName) {
        try {
            //switch(AppCxt.getParam("ERP")){
            //    case "T100":
            //        this.t100QueryMapper.createTable(tableName);
            //        break;
            //    case "GP":
            //        this.baseMapper.createTable(tableName);
            //        break;
            //    case "E10":
            //        this.baseMapper.createTable(tableName);
            //        break;
            //    case "YF":
            //        this.baseMapper.createTable(tableName);
            //        break;
            //    default:
            //        this.baseMapper.createTable(tableName);
            //        break;
            //}
            InvokeUtil.invokeBeanMethod("qryMapper,createTable",tableName);
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    @Transactional
    public void insertLS(List<Dto> list, String tableName) {
        for (Dto item : list) {
            Dto insert = Dtos.newDto();
            insert.put("LS001", item.getString("METHOD")); //方法
            insert.put("LS002", item.getString("DESCRIPTION")); //说明
            insert.put("LS003", item.getString("PARAMETER")); //传参
//            insert.put("LS004", item.getString("sql")); //查询sql
            insert.put("tableName", tableName);
            this.baseMapper.insertLS(insert);

        }
    }

    public List<Dto> getLsByGroup(Page page, Dto dto) {
        List<Dto> lsDto = new ArrayList<>();
        lsDto = this.baseMapper.getLsByGroup(page,dto);
        return lsDto;
    }

    public  void dropTable(String tablename){
        this.baseMapper.truncateTable(tablename);
        this.baseMapper.dropTable(tablename);
    }
}
